var server = require('../../utils/server');
var categoryId
var cPage = 0;
var gsort = "shop_price";
var asc = "desc";
// 使用function初始化array，相比var initSubMenuDisplay = [] 既避免的引用复制的，同时方式更灵活，将来可以是多种方式实现，个数也不定的
function initSubMenuDisplay() {
  return ['hidden', 'hidden', 'hidden', 'hidden'];
}

//定义初始化数据，用于运行时保存
var initSubMenuHighLight = [
  ['highlight', '', '', '', ''],
  ['', ''],
  ['', '', ''], []
];

Page({
  data: {
    menu: ["highlight", "", "", ""],
    subMenuDisplay: initSubMenuDisplay(),
    subMenuHighLight: initSubMenuHighLight,
    sort: [['shop_price-desc', 'shop_price-asc'], ['sales_sum-desc', 'sales_sum-asc'], ['is_new-desc', 'is_new-asc'], 'comment_count-asc'],
    goods: [],
    banner: [],
    coupon: [],
    bannerHeight: Math.ceil(386.0 / 640.0 * getApp().screenWidth),
    empty: false
  },

  onLoad: function (options) {
    categoryId = options.Id;
      this.getGoods(categoryId, 0, this.data.sort[0][0]);
  },

  getGoods: function (category, pageIndex, sort) {
    var that = this;
    var sortArray = sort.split('-');
    gsort = sortArray[0];
    asc = sortArray[1];


    server.getJSON('/Activity/activityList/id/' + category + "/sort/" + sortArray[0] + "/sort_asc/" + sortArray[1] + "/p/" + pageIndex, function (res) {

      // success
      var newgoods = res.data.result.goods_list;

      var couponlist = res.data.result.coupon_list;
      if (res.data.result.banner_list)
      {
      var bannerlist = res.data.result.banner_list;
      }
      else
      {

        var bannerlist = res.data.result.banner_listbig;
      }
      var ms = that.data.goods
      for (var i in newgoods) {
        ms.push(newgoods[i]);
      }

      if (ms.length == 0) {
        that.setData({
          empty: true
        });
      }
      else
        that.setData({
          empty: false
        });
      wx.stopPullDownRefresh();

      that.setData({
        banner: bannerlist,
        coupon: couponlist,
        goods: ms
      });


    });

  },


  tapGoods: function (e) {
    var objectId = e.currentTarget.dataset.objectId;
    wx.navigateTo({
      url: "../../../../goods/detail/detail?objectId=" + objectId
    });
  },
  tapMainMenu: function (e) {
    //		获取当前显示的一级菜单标识
    var index = parseInt(e.currentTarget.dataset.index);
    // 生成数组，全为hidden的，只对当前的进行显示
    var newSubMenuDisplay = initSubMenuDisplay();
    //		如果目前是显示则隐藏，反之亦反之。同时要隐藏其他的菜单
    if (this.data.subMenuDisplay[index] == 'hidden') {
      newSubMenuDisplay[index] = 'show';
    } else {
      newSubMenuDisplay[index] = 'hidden';
    }

    var menu = ["", "", "", ""];
    menu[index] = "highlight";

    if (index == 3) {
      this.setData({
        goods: []
      });
      cPage = 0;
        this.getGoods(categoryId, 0, this.data.sort[index]);
    }

    // 设置为新的数组
    this.setData({
      menu: menu,
      subMenuDisplay: newSubMenuDisplay
    });
  },
  tapSubMenu: function (e) {
    // 隐藏所有一级菜单
    this.setData({
      subMenuDisplay: initSubMenuDisplay()
    });
    // 处理二级菜单，首先获取当前显示的二级菜单标识
    var indexArray = e.currentTarget.dataset.index.split('-');
    // 初始化状态
    // var newSubMenuHighLight = initSubMenuHighLight;
    for (var i = 0; i < initSubMenuHighLight.length; i++) {
      // 如果点中的是一级菜单，则先清空状态，即非高亮模式，然后再高亮点中的二级菜单；如果不是当前菜单，而不理会。经过这样处理就能保留其他菜单的高亮状态
      //if (indexArray[0] == i) {
      for (var j = 0; j < initSubMenuHighLight[i].length; j++) {
        // 实现清空
        initSubMenuHighLight[i][j] = '';
      }
      // 将当前菜单的二级菜单设置回去
      //}
    }
    this.setData({
      goods: []
    });
    cPage = 0;
    
    this.getGoods(categoryId, 0, this.data.sort[indexArray[0]][indexArray[1]]);
   

    // 与一级菜单不同，这里不需要判断当前状态，只需要点击就给class赋予highlight即可
    initSubMenuHighLight[indexArray[0]][indexArray[1]] = 'highlight';
    // 设置为新的数组
    this.setData({
      subMenuHighLight: initSubMenuHighLight
    });
  },
  /*
  onReachBottom: function () {
      this.getGoods(categoryId, ++cPage, gsort + "-" + asc);
    wx.showToast({
      title: '加载中',
      icon: 'loading'
    })
  },*/
  onPullDownRefresh: function () {
    this.setData({
      goods: []
    });
    cPage = 0;
      this.getGoods(categoryId, cPage, gsort + "-" + asc);
  }
});